package com.kmxd.ams.app.watermark;

import cn.hutool.core.util.StrUtil;
import com.kmxd.ams.client.system.ISysConfigSvc;
import com.kmxd.ams.client.watermark.IWatermarkSvc;
import com.kmxd.ams.client.watermark.vo.WatermarkVO;
import com.kmxd.ams.core.common.constant.ConfigConst;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizTipException;
import com.kmxd.ams.core.util.ConfigUtil;
import java.io.File;
import java.util.Map;
import java.util.Objects;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * 水印工厂
 *
 * @author nickbi
 */
@Primary
@Service
@AllArgsConstructor
public class WatermarkSvcFactory implements IWatermarkSvc {
  private final ISysConfigSvc sysConfigSvc;

  private final Map<String, IWatermarkSvc> service;

  @Override
  public WatermarkVO addWatermark(Long archTypeId, File file, String waterMarkFilePath) {
    String contentByKey = sysConfigSvc.getConfigContentByKey(ConfigConst.OSS_OFD_PLUGIN);
    String view = ConfigUtil.getVal(contentByKey, ConfigConst.OFD_VIEW);
    if (StrUtil.isBlank(view)) {
      view = "pdfjs";
    }
    IWatermarkSvc instance = service.get(Constants.WATERMARK_SVC_PREFIX + view);
    if (Objects.isNull(instance)) {
      throw BizTipException.instance(ErrorCode.NOT_FOUND, "未找到添加水印实现");
    }
    WatermarkVO check = instance.addWatermark(archTypeId, file, waterMarkFilePath);
    return check;
  }
}
